summarise(dte_cycle_avg = mean(dte_election_max),
n_poll_cycle_avg = mean(n_poll, na.rm = T)) %>%
ungroup() %>%
mutate(timw_window = "All polls")
cycle_data100 <- polls %>%
group_by(cycle) %>%
summarise(dte_cycle_avg = mean(dte_election_max),
n_poll_cycle_avg = mean(n_poll, na.rm = T)) %>%
ungroup() %>%
mutate(timw_window = "100 days")
election_data100  <- polls100 %>%
group_by(state, cycle) %>%
summarise(dte_election_max = max(dte, na.rm = T),
n_poll = n()) %>%
ungroup() %>%
mutate(timw_window = "100 days")
cycle_data100 <- polls %>%
group_by(cycle) %>%
summarise(dte_cycle_avg = mean(dte_election_max),
n_poll_cycle_avg = mean(n_poll, na.rm = T)) %>%
ungroup() %>%
mutate(timw_window = "100 days")
cycle_data100 <- election_data100 %>%
group_by(cycle) %>%
summarise(dte_cycle_avg = mean(dte_election_max),
n_poll_cycle_avg = mean(n_poll, na.rm = T)) %>%
ungroup() %>%
mutate(timw_window = "100 days")
# combine
cycle_data_combined <- rbind(cycle_data, cycle_data100)
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1,
color = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1,
color = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1,
color = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 2,
color = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1,
color = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_path(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = 1)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = time_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour =  = timw_window)) +
# no. polls plot
n_polls_plot <- ggplot() +
# geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
#                                                colour = timw_window),
#             width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom") +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.77) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom",
legend.text = element_text(size = 16)) +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.7) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom",
legend.text = element_text(size = 16)) +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
# max. time window plot
avg_dte_plot <- ggplot() +
geom_jitter(data = election_data,
aes(x = cycle, y = dte_election_max), alpha = 0.7, width = 0.25) +
geom_path(data = cycle_data, aes(x = as.factor(cycle), y = as.numeric(dte_cycle_avg),
group = 1)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_blank()) +
labs(x = "", y = "Max. time window (days)")
avg_dte_plot
# max. time window plot
avg_dte_plot <- ggplot() +
geom_jitter(data = election_data,
aes(x = cycle, y = dte_election_max), alpha = 0.6, width = 0.25) +
geom_path(data = cycle_data, aes(x = as.factor(cycle), y = as.numeric(dte_cycle_avg),
group = 1)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_blank()) +
labs(x = "", y = "Max. time window (days)")
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.6) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom",
legend.text = element_text(size = 16)) +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
n_polls_plot
avg_dte_plot
# combine plots
avg_dte_n_poll_plot <- plot_grid(avg_dte_plot, n_polls_plot,
ncol = 1, align = "v",  rel_heights = c(1, 1.1), labels = "AUTO",
label_size = 16, label_x = 0.97, axis = "tblr")
# library(cowplot)
library(grid)
# combine plots
avg_dte_n_poll_plot <- plot_grid(avg_dte_plot, n_polls_plot,
ncol = 1, align = "v",  rel_heights = c(1, 1.1), labels = "AUTO",
label_size = 16, label_x = 0.97, axis = "tblr")
library(gridExtra)
# combine plots
avg_dte_n_poll_plot <- plot_grid(avg_dte_plot, n_polls_plot,
ncol = 1, align = "v",  rel_heights = c(1, 1.1), labels = "AUTO",
label_size = 16, label_x = 0.97, axis = "tblr")
{
library(ggplot2)
library(dplyr)
library(cowplot)
# library(grid)
# library(gridExtra)
# library(stringi)
# library(ggrepel)
# library(usdata)
}
# combine plots
avg_dte_n_poll_plot <- plot_grid(avg_dte_plot, n_polls_plot,
ncol = 1, align = "v",  rel_heights = c(1, 1.1), labels = "AUTO",
label_size = 16, label_x = 0.97, axis = "tblr")
avg_dte_n_poll_plot
# max. time window plot
avg_dte_plot <- ggplot() +
geom_jitter(data = election_data,
aes(x = cycle, y = dte_election_max), alpha = 0.6, width = 0.25) +
geom_path(data = cycle_data, aes(x = as.factor(cycle), y = as.numeric(dte_cycle_avg),
group = 1)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_blank(),
plot.margin=grid::unit(c(2,10,2,12), "mm")) +
labs(x = "", y = "Max. time window (days)")
# no. polls plot
n_polls_plot <- ggplot() +
geom_jitter(data = election_data_combined, aes(x = cycle, y = n_poll,
colour = timw_window),
width = 0.25, alpha = 0.6) +
geom_line(data = cycle_data_combined, aes(x = as.factor(cycle),
y = n_poll_cycle_avg,
group = timw_window,
colour = timw_window)) +
theme_bw() +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom",
legend.text = element_text(size = 16),
plot.margin=grid::unit(c(2,10,2,12), "mm")) +
labs(x = "", y = "No. polls", colour = "") +
scale_color_manual(values = c("darkgrey", "black"))
# combine plots
avg_dte_n_poll_plot <- plot_grid(avg_dte_plot, n_polls_plot,
ncol = 1, align = "v",  rel_heights = c(1, 1.2), labels = "AUTO",
label_size = 16, label_x = 0.97, axis = "tblr")
avg_dte_n_poll_plot
# save
ggsave(filename = 'code/results_vis/plots/fgA2.eps',
plot = avg_dte_n_poll_plot,
width = 14, height = 8, bg='#ffffff', device=cairo_ps, dpi = 1200)
?cut.Date
# weekly data
polls_week <- polls %>% cut.Date(dte, breaks = week)
View(polls)
# weekly data
polls_week <- polls %>%
mutate(wte = difftime(strptime(end_date, format = "%m/%d/%Y"),
strptime(election_date, format = "%m/%d/%Y"),units="weeks"))
View(polls)
View(polls_week)
# weekly data
polls_week <- polls %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%Y"),
strptime(end_date, format = "%m/%d/%Y"), units="weeks"))
View(polls_week)
# weekly data
polls_week <- polls %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%Y"),
strptime(end_date, format = "%m/%d/%Y"), units="weeks") %>%
round(0))
View(polls_week)
# weekly data
polls_week <- polls %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%y"),
strptime(end_date, format = "%m/%d/%y"), units="weeks") %>%
round(0))
View(polls_week)
polls <- readRDS("data/us_senate_polls1990_2022_final.RDS")
# remove polls conducted before the porevious election
polls <- polls %>%
filter(dte < 1461)  %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%y"),
strptime(end_date, format = "%m/%d/%y"), units="weeks") %>%
round(0))
# weekly data
polls_week <- polls %>%
group_by(cycle, wte) %>%
summarise(n_poll) %>%
ungroup()
# weekly data
polls_week <- polls %>%
group_by(cycle, wte) %>%
summarise(n_poll = n()) %>%
ungroup()
n_poll_week_plot <- ggplot(polls_week) +
geom_line(x = wte) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls")
# remove polls conducted before the porevious election
polls <- polls %>%
filter(dte < 1461)  %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%y"),
strptime(end_date, format = "%m/%d/%y"), units="weeks") %>%
round(0))
# weekly data
polls_week <- polls %>%
group_by(cycle, wte) %>%
summarise(n_poll = n()) %>%
ungroup()
n_poll_week_plot <- ggplot(polls_week) +
geom_line(x = wte) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16))
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16))
n_poll_week_plot
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, group = 1)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16))
n_poll_week_plot
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, y = n_poll)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16))
n_poll_week_plot
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, y = n_poll)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16)) +
scale_x_reverse()
n_poll_week_plot
?round
ceiling(8.1)
ceiling(0.7)
floor(0.7)
polls <- readRDS("data/us_senate_polls1990_2022_final.RDS")
# remove polls conducted before the porevious election
polls <- polls %>%
filter(dte < 1461)  %>%
mutate(wte = difftime(strptime(election_date, format = "%m/%d/%y"),
strptime(end_date, format = "%m/%d/%y"), units="weeks") %>%
floor())
# weekly data
polls_week <- polls %>%
group_by(cycle, wte) %>%
summarise(n_poll = n()) %>%
ungroup()
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, y = n_poll)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16)) +
scale_x_reverse()
n_poll_week_plot
# save
ggsave(filename = 'code/results_vis/plots/fgA1.eps',
plot = n_poll_week_plot,
width = 14, height = 10, bg='#ffffff', device=cairo_ps, dpi = 1200)
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, y = n_poll)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45),
strip.text = element_text(size = 16),
axis.title.x = element_text(margin = margin(t = 6))) +
scale_x_reverse()
# save
ggsave(filename = 'code/results_vis/plots/fgA1.eps',
plot = n_poll_week_plot,
width = 14, height = 10, bg='#ffffff', device=cairo_ps, dpi = 1200)
# number of polls per week
n_poll_week_plot <- ggplot(polls_week) +
geom_line(aes(x = wte, y = n_poll)) +
facet_wrap(~ cycle, ncol = 3) +
theme_bw() +
labs(x = "Weeks to election", y = "No. polls") +
theme(text = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_text(angle = 45, margin = margin(t = 6)),
strip.text = element_text(size = 16)) +
scale_x_reverse()
# save
ggsave(filename = 'code/results_vis/plots/fgA1.eps',
plot = n_poll_week_plot,
width = 14, height = 10, bg='#ffffff', device=cairo_ps, dpi = 1200)
